package io.github.uicdb.liba.api.register.data;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.github.uicdb.liba.Liba;
import io.github.uicdb.liba.api.register.ItemHolder;
import net.minecraft.data.DataGenerator;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraftforge.client.model.generators.ItemModelProvider;
import net.minecraftforge.common.data.ExistingFileHelper;

import javax.annotation.Nonnull;
import java.util.Map;
import java.util.function.BiConsumer;

public class LazyItemModelProvider extends ItemModelProvider {
    private static final ResourceLocation SPECIAL_BLOCK_PARENT = new ResourceLocation(Liba.MODID, "item/base/special_block");
    private static final ResourceLocation SPAWN_EGG_PARENT = new ResourceLocation("minecraft", "item/template_spawn_egg");
    private final Map<ItemHolder<?>, BiConsumer<ItemModelProvider, ItemHolder<?>>> iItemModelMap;
    private final String modid;
    private static final Gson GSON = (new GsonBuilder()).setPrettyPrinting().create();
    public LazyItemModelProvider(DataGenerator generator, String modid, ExistingFileHelper fileHelper, Map<ItemHolder<?>, BiConsumer<ItemModelProvider, ItemHolder<?>>> iItemModelMap) {
        super(generator, modid, fileHelper);
        this.iItemModelMap = iItemModelMap;
        this.modid = modid;
    }

    @Override
    protected void registerModels() {
        iItemModelMap.forEach((itemHolder, iItemModel) -> {
            var item = itemHolder.get();
            var id = item.getRegistryName();
          if (item instanceof SpawnEggItem) {
                this.withExistingParent(id.getPath(), SPAWN_EGG_PARENT);
            } else {
                iItemModel.accept(this, itemHolder);
            }
        });
    }
    @Nonnull
    @Override
    public String getName() {
        return "LazyItemModelProvider For" + modid;
    }
}
